#include "progression.h"

Progression::Progression()
{
    m_repeat = REPEAT_NONE;
}

Progression::~Progression()
{
    m_songs.clear();
    m_played.clear();
}

void Progression::add(int pos){
    //printf("Progression::add(%d)\n", pos);
    if(m_played.contains(pos) || m_songs.contains(pos)){
        for(int i = 0; i < m_songs.size(); i++){
            if(m_songs[i] >= pos)
                ++m_songs[i];

        }
        for(int i = 0; i < m_played.size(); i++){
            if(m_played[i] >= pos)
                ++m_played[i];

        }
    }
    m_songs.push_back(pos);
    //print();
}

bool Progression::next(){
    if(m_repeat == REPEAT_NONE){
        if(m_songs.size() > 2){
            m_played.push_front(m_songs.takeFirst());
            return true;
        }else
            return false;
    }else if(m_repeat == REPEAT_ONE)
        return true;
    else if(m_repeat == REPEAT_ALL){
        if(m_songs.size() > 2){
            m_played.push_front(m_songs.takeFirst());
            return true;
        }else{
            while(m_played.size())
                m_songs.push_front(m_played.takeFirst());
            return true;
        }
    }
    //This should not be reached. Just pleasing the compiler.
    return false;
}

bool Progression::prev(){
    if(m_repeat == REPEAT_NONE){
        if(m_played.size() > 0){
            m_songs.push_front(m_played.takeFirst());
            return true;
        }else
            return false;
    }else if(m_repeat == REPEAT_ONE)
        return true;
    else if(m_repeat == REPEAT_ALL){
        if(m_played.size() > 0){
            m_songs.push_front(m_played.takeFirst());
            return true;
        }else{
            m_played.push_front(m_songs.takeFirst());
            return true;
        }

    }
    //This should not be reached. Just pleasing the compiler.
    return false;

}

short int Progression::current()const{
    if(m_songs.size() > 0)
        return m_songs[0];
    else
        return 0;//Not sure what else to do here, but it should never happen.
}

void Progression::print()const{
    printf("m_songs:\n");
    for(int i = 0; i < m_songs.size(); i++)
        printf("%d ", m_songs[i]);
    printf("\n");
    printf("m_played:\n");
    for(int i = 0; i < m_played.size(); i++)
        printf("%d ", m_played[i]);
    printf("\n");

}
